import reshandler from 'services/response-handler'
/*
 * 查询新的车友动态
 */
function makeData(response) {
  return Object.assign({},response, {
    spaceMessages: response.spaceMessages || [],
    lastestTime: response.lastestTime || getCurrentDate()
  })
}

function getCurrentDate() {
  var date = new Date();
  var year = date.getFullYear() + "";
  var month = cusPadStart((date.getMonth() + 1).toString(), 2, "0");
  var day = cusPadStart(date.getDate().toString(), 2, "0");
  var hour = cusPadStart(date.getHours().toString(), 2, "0");
  var minute = cusPadStart(date.getMinutes().toString(), 2, "0");
  var second = cusPadStart(date.getSeconds().toString(), 2, "0");
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}

/**
 * padstart方法
 */
function cusPadStart(str, len, strPre) {
  if (String.prototype.padStart) {
    str = str.padStart(len, strPre);
  } else {
    let tempLen = len - str.length;
    for (let index = 0; index < tempLen; index++) {
      str = strPre + str;
    }
  }
  return str;
}

export default function (params, success, fail) {
  // request 
  params = Object.assign({}, params, {
    methodName: "QuerySpaceMessage",
  })

  // invoke
  this.$.get(params)
    .then(reshandler.success(success, fail, makeData), reshandler.error(fail))
}
